//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls 
{
    // *********************************************************************
    //  ForumGroupView
    //
    /// <summary>
    /// This server control is used to display top level threads. Note, a thread
    /// is a post with more information. The Thread class inherits from the Post
    /// class.
    /// </summary>
    /// 
    // ********************************************************************/
    public class ForumGroupView : TemplatedWebControl
    {
        #region Child Controls

        protected Repeater repeater;
		HtmlAnchor togglePersonalize;
        IText forumGroupName;
        HtmlGenericControl forumGroupDetails, topBreadCrumb;

        CSContext csContext = CSContext.Current;
        ControlUserMode mode = ControlUserMode.User;

        #endregion

        protected override void OnInit(EventArgs e) 
        {
            if (SkinName == null)                
                ExternalSkinFileName = "View-ForumGroupView.ascx";
            else 
                ExternalSkinFileName = SkinName;
			
            base.OnInit(e);
        }
		
        protected override void OnLoad(EventArgs e) 
        {
            DataBind();

            base.OnLoad(e);
        }

        #region DataBind

        public override void DataBind() 
        {
            base.DataBind();

            if (forumGroupName != null)
                forumGroupName.Text = ResourceManager.GetString("discussions");


			if (togglePersonalize != null)
			{
				if (!csContext.User.IsAnonymous)
				{
					if (csContext.QueryString["personalize"] != "true") 
					{
						togglePersonalize.HRef = Globals.GetSiteUrls().ForumPersonalize;
						togglePersonalize.InnerText = ResourceManager.GetString("ForumGroupView_Personalize_Manage");
					}
					else
					{
						togglePersonalize.HRef = Globals.GetSiteUrls().ForumsHome;
						togglePersonalize.InnerText = ResourceManager.GetString("ForumGroupView_Personalize_View");
					}
				}
				else
					togglePersonalize.Visible = false;
			}

            ArrayList forumGroups = null;

            // Don't data bind if the data source is already populated
            //
            if (repeater.DataSource != null)
                return;

            // Get all forum groups if we're auto-databinding
            // we always cache
            //
            switch (Mode) 
            {

                case ControlUserMode.Administrator:
                    forumGroups = ForumGroups.GetForumGroups(false);

                    // Ignore collapse/expand information
                    foreach (Group group in forumGroups)
                        group.HideSections = false;

                    break;

                case ControlUserMode.Moderator:
                    forumGroups = ForumGroups.GetForumGroups(false,false,true, true);

                    // Ignore collapse/expand information
                    foreach (Group group in forumGroups)
                        group.HideSections = false;

                    break;

                default:
					if(csContext.QueryString["personalize"] == "true" || csContext.GroupID > 0)
					{
						forumGroups = ForumGroups.GetForumGroups(true);
					}
					else
					{
						forumGroups = ForumGroups.GetFilteredForumGroups();
					}


                    break;


            }

            // Have we asked for a particular forum group?
            //
            if (csContext.GroupID > 0) 
            {
                ArrayList newForumGroups = new ArrayList();
				
                // Try to find the requested forum group
                foreach (Group f in forumGroups) 
                {
                    if (f.GroupID == csContext.GroupID) 
                    {
                        f.HideSections = false;
                        newForumGroups.Add(f);
                        Head.AddSiteNameTitle(f.Name,Context);
						if (ForumGroups.IsPublic(f.GroupID) && Mode == ControlUserMode.User)
                        {
                            UsersOnline.SetLocation(f.Name);
                        }
                        else
                        {
                            UsersOnline.PrivateLocation();
                        }

                        break;
                    }
                }

                forumGroups = newForumGroups;

                // Display details for selected forum group
                //
                if (forumGroupDetails != null)
                {
                    forumGroupDetails.Visible = true;

     
                }

				if (forumGroupName != null && forumGroups.Count > 0) 
				{
					forumGroupName.Text = ((Group) forumGroups[0]).Name;

					if(csContext.User.IsForumAdministrator)
					{
						InlineTextEdit inline = forumGroupName as InlineTextEdit;
						if(inline != null)
						{
							AjaxManager.Register(this,"QuickForumGroupEdit");
							inline.AjaxClientID = this.ClientID;
							inline.AjaxFunction= string.Format("QuickForumGroupEdit.UpdateForumGroupName('{0}',this.innerHTML,null);",this.ClientID);

						}
					}
				}

                if (topBreadCrumb != null)
                {
                    topBreadCrumb.Visible = true;
                }
            }
            else
            {
				if (Mode == ControlUserMode.User)
				{
					if (csContext.QueryString["personalize"] == "true")
					{
						Head.AddSiteNameTitle(ResourceManager.GetString("ForumGroupView_Personalize_Manage"), Context);
						UsersOnline.SetLocation(ResourceManager.GetString("ForumGroupView_Personalize_Manage"));
					}
					else
					{
						Head.AddSiteNameTitle(ResourceManager.GetString("forums"), Context);
						UsersOnline.SetLocation(ResourceManager.GetString("forums"));
					}
				}
            }

            // Databind if we have values
            //
            if (forumGroups.Count > 0) 
            {
                repeater.DataSource = forumGroups;
                repeater.DataBind();                
            }
        }

		[AjaxMethod(IncludeControlValuesWithCallBack=false)]
		public string ToggleGroupVisiblity(int groupID, string text)
		{
			return text;
		}

		[AjaxMethod(IncludeControlValuesWithCallBack=false)]
		public void UpdateForumGroupName(string name)
		{
			if(csContext.User.IsForumAdministrator)
			{
				Group g = ForumGroups.GetForumGroup(csContext.GroupID);
				if(g != null)
				{
					
					g.Name = name;
					ForumGroups.Update(g);
				}
			}
		}
		
        #endregion		
		
        #region Skin
                
        protected override void AttachChildControls() 
        {
            forumGroupDetails = FindControl("ForumGroupDetails") as HtmlGenericControl;
            topBreadCrumb = FindControl("TopBreadCrumb") as HtmlGenericControl;
            forumGroupName = TextControlHelper.Create(FindControl("ForumGroupName"));
            repeater = (Repeater) FindControl("forumGroupRepeater");
			togglePersonalize = FindControl("Personalize") as HtmlAnchor;
			
            InitializeChildControls();
        }

        private void InitializeChildControls() 
        {
            // Add an item command to the forum group repeater
            //
//            if (repeater != null)
//                repeater.ItemCreated += new RepeaterItemEventHandler(Repeater_ItemCreated);		
        }
		        
        #endregion

        #region Properties

        public ControlUserMode Mode 
        {
            get { return mode; }
            set { mode = value; }
        }

        #endregion

        #region Event Handlers

//        private void Repeater_ItemCreated(Object sender, RepeaterItemEventArgs e) 
//        {
//            if (e.Item.ItemType == ListItemType.Item ||
//                e.Item.ItemType == ListItemType.AlternatingItem) 
//            {
//                ImageButton b = (ImageButton) e.Item.FindControl("ExpandCollapse");
//
//                if (b != null) 
//                {
//                    /*
//					if (csContext.ForumGroupID > 0) {
//						b.Visible = false;
//					} 
//					else {
//						b.Command += new CommandEventHandler(ExpandCollapse_Command);
//					}*/
//
//                    if (csContext.User.EnableCollapsingPanels && 
//                        csContext.SiteSettings.EnableCollapsingPanels &&
//                        (csContext.GroupID <= 0)) 
//                    {
//
//                        b.Visible = true;
//                        b.Command += new CommandEventHandler(ExpandCollapse_Command);
//                    }
//                    else 
//                        b.Visible = false;
//                }
//
//           }
//        }

//        public void ExpandCollapse_Command(object sender, CommandEventArgs e) 
//        {
//            ArrayList groups = (ArrayList) repeater.DataSource;
//            int fgID = Convert.ToInt32( e.CommandArgument );
//            UserCookie userCookie = csContext.User.GetUserCookie();
//
//            // Run through each group and set the appropriate display
//            //
//            foreach (Group g in groups) 
//            {
//                if (g.GroupID == fgID) 
//                {
//                    if (g.HideSections) 
//                    {
//                        g.HideSections = false;
//                        userCookie.RemoveHiddenForumGroup(g.GroupID);
//                    } 
//                    else 
//                    {
//                        userCookie.AddHiddenForumGroup(g.GroupID);
//                        g.HideSections = true;
//                    }
//                }
//
//            }
//
//            if (HttpContext.Current != null) 
//            {
//                HttpContext context = HttpContext.Current;
//                context.Response.Redirect( context.Request.RawUrl );
//            } 
//            else 
//            {
//                repeater.DataSource = groups;
//                repeater.DataBind();
//            }
//        }

        #endregion
    }
}